package cc.cix.gt.org.controller;

import cc.cix.gt.common.constant.StringConstant;
import cc.cix.gt.common.entity.ResultPage;
import cc.cix.gt.common.util.SystemUtil;
import cc.cix.gt.org.dto.query.QueryLoginLogReq;
import cc.cix.gt.org.entity.LoginLog;
import cc.cix.gt.org.service.ILoginLogService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cix
 */
@Api(tags = "登录日志管理")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/loginLog")
public class LoginLogController {

    private final ILoginLogService loginLogService;

    @ApiOperation("登录日志列表")
    @GetMapping
    public ResultPage<LoginLog> listLoginLog(QueryLoginLogReq dto) {
        return loginLogService.listLoginLog(dto);
    }

    @ApiOperation("用户最近7次登录日志")
    @GetMapping("/currentUser")
    public List<LoginLog> getUserLastSevenLoginLogs() {
        String currentUsername = SystemUtil.getCurrentUser().getUsername();
        return loginLogService.listUserLastSevenLoginLogs(currentUsername);
    }

    @ApiOperation("删除登录日志")
    @PreAuthorize("hasAuthority('loginlog:delete')")
    @DeleteMapping("/{ids}")
    public void deleteLogs(@PathVariable String ids) {
        loginLogService.removeByIds(StrUtil.splitTrim(ids, StringConstant.COMMA));
    }

}
